<!--
 * @Author: your name
 * @Date: 2021-06-24 16:59:15
 * @LastEditTime: 2021-07-02 19:05:58
 * @LastEditors: jiangsx
 * @Description: In User Settings Edi
 * @FilePath: \mine\briup-ui\briup-ui\src\pages\admin\custome\List.vue
-->
<template>
  <!-- 顾客管理 -->
  <div class="custome-list">
    <div class="search">
      <div>
        <el-button type="primary" class="btn" @click="toAdd">添加</el-button>
      </div>
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item>
          <el-input
            v-model="searchForm.username"
            clearable
            placeholder="用户名"
          />
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="searchForm.status"
            clearable
            placeholder="用户状态"
          >
            <el-option label="正常" value="正常" />
            <el-option label="禁用" value="禁用" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="toSearch">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div>
      <el-table v-loading="loading" :data="tableData" style="width: 100%">
        <el-table-column align="center" prop="id" label="编号" />
        <el-table-column align="center" prop="username" label="用户名" />
        <el-table-column align="center" prop="realname" label="姓名" />
        <el-table-column align="center" prop="telephone" label="手机号" />
        <el-table-column align="center" prop="gender" label="性别" />
        <el-table-column align="center" label="状态">
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.status === '正常' ? 'success' : 'danger'"
            >{{ scope.row.status }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="140">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="toEdit(scope.row)"
            >编辑</el-button>
            <el-button
              type="text"
              size="small"
              @click="toDelete(scope.row.id)"
            >删除</el-button>
            <el-button
              type="text"
              size="small"
              @click="toDetails(scope.row.id)"
            >详情</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-div">
      <el-pagination
        :current-page="params.page"
        :page-sizes="[5, 10, 15, 25]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <div>
      <el-dialog :title="title" :visible.sync="visible" @close="toClose">
        <el-form ref="ruleForm" :model="form" :rules="rules">
          <el-form-item
            label="用户名"
            prop="username"
            :label-width="formLabelWidth"
          >
            <el-input v-model="form.username" />
          </el-form-item>
          <el-form-item
            label="姓名"
            prop="realname"
            :label-width="formLabelWidth"
          >
            <el-input v-model="form.realname" />
          </el-form-item>
          <el-form-item
            prop="telephone"
            label="手机号"
            :label-width="formLabelWidth"
          >
            <el-input v-model.number="form.telephone" autocomplete="off" />
          </el-form-item>
          <el-form-item
            prop="password"
            label="密码"
            :label-width="formLabelWidth"
          >
            <el-input v-model="form.password" autocomplete="off" />
          </el-form-item>
          <el-form-item label="性别" :label-width="formLabelWidth">
            <el-radio-group v-model="form.gender">
              <el-radio label="male">男</el-radio>
              <el-radio label="female">女</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="状态" :label-width="formLabelWidth">
            <el-radio-group v-model="form.status">
              <el-radio label="正常">正常</el-radio>
              <el-radio label="禁用">禁用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="visible = false">取 消</el-button>
          <el-button type="primary" @click="toSave">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { pageQuery, addUserWidthRole, deleteById } from '@/api/user'

export default {
  data() {
    return {
      searchForm: {},
      tableData: [],
      params: {
        page: 1,
        pageSize: 10
      },
      roleId: 6,
      total: 0,
      loading: false,
      visible: false,
      form: {},
      formLabelWidth: '80px',
      title: '',
      rules: {
        username: [
          {
            required: true,
            message: '请输入姓名',
            trigger: 'blur'
          }
        ],
        realname: [
          {
            required: true,
            message: '请输入姓名',
            trigger: 'blur'
          }
        ],
        telephone: [
          {
            required: true,
            message: '请输入手机号',
            trigger: 'blur'
          }
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  computed: {},
  watch: {
    params: {
      handler() {
        this.queryData()
      },
      deep: true
    }
  },
  created() {
    this.queryData()
  },
  mounted() {},
  methods: {
    async queryData() {
      this.loading = true
      const temp = {
        roleId: 6,
        ...this.params,
        ...this.searchForm
      }
      for (const key in temp) {
        if (!temp[key]) {
          delete temp[key]
        }
      }
      const res = await pageQuery(temp)
      this.loading = false
      this.tableData = res.data.list
      this.total = res.data.total
    },
    toSearch() {
      this.queryData()
    },
    handleSizeChange(val) {
      this.params = {
        page: 1,
        pageSize: val
      }
    },
    handleCurrentChange(val) {
      this.params.page = val
    },
    toAdd() {
      this.title = '添加顾客信息'
      this.visible = true
      this.form = {}
    },
    toSave() {
      this.$refs['ruleForm'].validate(async(valid) => {
        if (valid) {
          const obj = {
            roleId: 6,
            ...this.form
          }
          const res = await addUserWidthRole(obj)
          this.$notify.success({
            title: '成功',
            message: res.message
          })
          this.queryData()
          this.visible = false
        } else {
          return false
        }
      })
    },
    toClose() {
      this.$refs.ruleForm.resetFields()
      setTimeout(() => {
        this.visible = false
      }, 200)
    },
    toEdit(row) {
      this.title = '编辑顾客信息'
      this.visible = true
      this.form = { ...row }
    },
    async toDelete(id) {
      const res = await deleteById({ id })
      this.$notify.success({
        title: '成功',
        message: res.message
      })
      this.queryData()
    },
    toDetails(id) {
      this.$router.push({
        path: '/admin/custome/Details',
        query: {
          index: id
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.custome-list {
  padding: 10px;
}
.search {
  display: flex;
  .btn {
    margin-right: 10px;
  }
}
</style>
